package com.strato.mvc.view.json.v1_0_0;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.strato.json.v1_0_0.exec.JSONUtils;
import com.strato.mvc.api.v1_0_0.view.View;

public class JsonView implements View {

	private Object object;

	public JsonView(Object object){
		this.object=object;
	}
	
	@Override
	public void render(HttpServletRequest request, HttpServletResponse response, Map<?, ?> model) {
		try {
			PrintWriter out = response.getWriter();
			String json = "";			
			if(object != null){
				json = JSONUtils.getInstance().toJSON(object);
			}
			out.println(json);
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	public static JsonView fromObject(Object object){
		return new JsonView(object);
	}
	
}
